home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-25  |  10.1 KB  |  448 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. #include <config.h>
  23. #include <stdlib.h>
  24. #include "video.h"
  25. #include "proto.h"
  26. #include "util.h"
  27.  
  28. /* Declarations of global variables used. */
  29.  
  30. unsigned int curBits;
  31. int bitOffset;
  32. int bufLength;
  33. unsigned int *bitBuffer;
  34.  
  35. /* Bit masks used by bit i/o operations. */
  36.  
  37. unsigned int nBitMask[] = { 0x00000000, 0x80000000, 0xc0000000, 0xe0000000, 
  38.                 0xf0000000, 0xf8000000, 0xfc000000, 0xfe000000, 
  39.                 0xff000000, 0xff800000, 0xffc00000, 0xffe00000, 
  40.                 0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000, 
  41.                 0xffff0000, 0xffff8000, 0xffffc000, 0xffffe000, 
  42.                 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00, 
  43.                 0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0, 
  44.                 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe};
  45.  
  46. unsigned int bitMask[] = {  0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff, 
  47.                 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
  48.                 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
  49.                 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
  50.                 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
  51.                 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
  52.                 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
  53.                 0x0000000f, 0x00000007, 0x00000003, 0x00000001};
  54.  
  55. unsigned int rBitMask[] = { 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8, 
  56.                 0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80, 
  57.                 0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800, 
  58.                 0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000, 
  59.                 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000, 
  60.                 0xfff00000, 0xffe00000, 0xffc00000, 0xff800000, 
  61.                 0xff000000, 0xfe000000, 0xfc000000, 0xf8000000, 
  62.                 0xf0000000, 0xe0000000, 0xc0000000, 0x80000000};
  63.  
  64. unsigned int bitTest[] = {  0x80000000, 0x40000000, 0x20000000, 0x10000000, 
  65.                 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  66.                 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  67.                 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  68.                 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  69.                 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  70.                 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  71.                 0x00000008, 0x00000004, 0x00000002, 0x00000001};
  72.  
  73.  
  74. /*
  75.  *--------------------------------------------------------------
  76.  *
  77.  * correct_underflow --
  78.  *
  79.  *    Called when buffer does not have sufficient data to 
  80.  *      satisfy request for bits.
  81.  *      Calls get_more_data, an application specific routine
  82.  *      required to fill the buffer with more data.
  83.  *
  84.  * Results:
  85.  *      None really.
  86.  *  
  87.  * Side effects:
  88.  *    buf_length and buffer fields in curVidStream structure
  89.  *      may be changed.
  90.  *
  91.  *--------------------------------------------------------------
  92.  */
  93.  
  94. void 
  95. correct_underflow() {
  96.  
  97.   int status;
  98.  
  99.   status = get_more_data(curVidStream->buf_start,
  100.              curVidStream->max_buf_length,
  101.              &bufLength, &bitBuffer);
  102.  
  103.   if (status  < 0) {
  104.     fprintf (stderr, "\n");
  105.     perror("Unexpected read error.");
  106.     exit(1);
  107.   }
  108.   else if ((status == 0) && (bufLength < 1)) {
  109.     fprintf(stderr, "\nImproper or missing sequence end code.\n");
  110.  
  111. /*  if (loopFlag) longjmp(env, 1);   */
  112.     DestroyVidStream(curVidStream);
  113.     exit(0);
  114.   }
  115. #ifdef UTIL2
  116.   curBits = *bitBuffer << bitOffset;
  117. #else
  118.   curBits = *bitBuffer;
  119. #endif
  120.  
  121. }
  122.  
  123.  
  124. /*
  125.  *--------------------------------------------------------------
  126.  *
  127.  * next_bits --
  128.  *
  129.  *    Compares next num bits to low order position in mask.
  130.  *      Buffer pointer is NOT advanced.
  131.  *
  132.  * Results:
  133.  *    TRUE, FALSE, or error code.
  134.  *
  135.  * Side effects:
  136.  *    None.
  137.  *
  138.  *--------------------------------------------------------------
  139.  */
  140.  
  141. int next_bits(num, mask)
  142. int num;
  143. unsigned int mask;
  144. {
  145.   unsigned int stream;
  146.   int ret_value;
  147.  
  148.   /* If no current stream, return error. */
  149.  
  150.   if (curVidStream == NULL)
  151.     return NO_VID_STREAM;
  152.  
  153.   /* Get next num bits, no buffer pointer advance. */
  154.  
  155.   show_bitsn(num, stream);
  156.  
  157.   /* Compare bit stream and mask. Set return value toTRUE if equal, FALSE if
  158.      differs. 
  159.   */
  160.  
  161.   if (mask == stream) {
  162.     ret_value = TRUE;
  163.   } else ret_value = FALSE;
  164.  
  165.   /* Return return value. */
  166.  
  167.   return ret_value;
  168. }
  169.  
  170.  
  171. /*
  172.  *--------------------------------------------------------------
  173.  *
  174.  * get_ext_data --
  175.  *
  176.  *    Assumes that bit stream is at begining of extension
  177.  *      data. Parses off extension data into dynamically 
  178.  *      allocated space until start code is hit. 
  179.  *
  180.  * Results:
  181.  *    Pointer to dynamically allocated memory containing
  182.  *      extension data.
  183.  *
  184.  * Side effects:
  185.  *    Bit stream irreversibly parsed.
  186.  *
  187.  *--------------------------------------------------------------
  188.  */
  189.  
  190. char *get_ext_data ()
  191. {
  192.   int size, marker;
  193.   char *dataPtr;
  194.   unsigned int data;
  195.  
  196.   /* Set initial ext data buffer size. */
  197.  
  198.   size = EXT_BUF_SIZE;
  199.  
  200.   /* Allocate ext data buffer. */
  201.  
  202.   dataPtr = (char *) malloc(size);
  203.  
  204.   /* Initialize marker to keep place in ext data buffer. */
  205.  
  206.   marker = 0;
  207.  
  208.   /* While next data is not start code... */
  209.   while (!next_bits(24, 0x000001)) {
  210.  
  211.     /* Get next byte of ext data. */
  212.  
  213.     get_bits8(data);
  214.  
  215.     /* Put ext data into ext data buffer. Advance marker. */
  216.  
  217.     dataPtr[marker] = (char) data;
  218.     marker++;
  219.  
  220.     /* If end of ext data buffer reached, resize data buffer. */
  221.  
  222.     if (marker == size) {
  223.       size += EXT_BUF_SIZE;
  224.       dataPtr = (char *) realloc(dataPtr, size);
  225.     }
  226.   }
  227.  
  228.   /* Realloc data buffer to free any extra space. */
  229.  
  230.   dataPtr = (char *) realloc(dataPtr, marker);
  231.  
  232.   /* Return pointer to ext data buffer. */
  233.  
  234.   return dataPtr;
  235. }
  236.  
  237.  
  238. /*
  239.  *--------------------------------------------------------------
  240.  *
  241.  * next_start_code --
  242.  *
  243.  *    Parses off bitstream until start code reached. When done
  244.  *      next 4 bytes of bitstream will be start code. Bit offset
  245.  *      reset to 0.
  246.  *
  247.  * Results:
  248.  *    Status code.
  249.  *
  250.  * Side effects:
  251.  *    Bit stream irreversibly parsed.
  252.  *
  253.  *--------------------------------------------------------------
  254.  */
  255.  
  256. int next_start_code()
  257. {
  258.   int state;
  259.   int byteoff;
  260.   unsigned int data;
  261.  
  262.   /* If no current stream, return error. */
  263.  
  264.   if (curVidStream == NULL)
  265.     return NO_VID_STREAM;
  266.  
  267.   /* If insufficient buffer length, correct underflow. */
  268.  
  269.   if (bufLength < 2) {
  270.     correct_underflow();
  271.   }
  272.  
  273.   /* If bit offset not zero, reset and advance buffer pointer. */
  274.  
  275.   byteoff = bitOffset % 8;
  276.  
  277.   if (byteoff != 0) {
  278.     flush_bits((8-byteoff));
  279.   }
  280.  
  281.   /* Set state = 0. */
  282.  
  283.   state = 0;
  284.  
  285.   /* While buffer has data ... */
  286.  
  287.   while(bufLength > 0) {
  288.  
  289.     /* If insufficient data exists, correct underflow. */
  290.  
  291.     if (bufLength < 2) {
  292.       correct_underflow();
  293.     }
  294.  
  295.     /* If next byte is zero... */
  296.  
  297.     get_bits8(data);
  298.  
  299.     if (data == 0) {
  300.  
  301.       /* If state < 2, advance state. */
  302.  
  303.       if (state < 2) state++;
  304.     }
  305.  
  306.     /* If next byte is one... */
  307.  
  308.     else if (data == 1) {
  309.  
  310.       /* If state == 2, advance state (i.e. start code found). */
  311.  
  312.       if (state == 2) state++;
  313.  
  314.       /* Otherwise, reset state to zero. */
  315.  
  316.       else state = 0;
  317.     }
  318.  
  319.     /* Otherwise byte is neither 1 or 0, reset state to 0. */
  320.  
  321.     else {
  322.       state = 0;
  323.     }
  324.  
  325.     /* If state == 3 (i.e. start code found)... */
  326.  
  327.     if (state == 3) {
  328.  
  329.       /* Set buffer pointer back and reset length & bit offsets so
  330.      next bytes will be beginning of start code. 
  331.       */
  332.  
  333.       bitOffset = bitOffset - 24;
  334.  
  335.       if (bitOffset < 0) {
  336.     bitOffset = 32 + bitOffset;
  337.     bufLength++;
  338.     bitBuffer--;
  339. #ifdef UTIL2
  340.     curBits = *bitBuffer << bitOffset;
  341. #else
  342.     curBits = *bitBuffer;
  343. #endif
  344.       }
  345.       else {
  346. #ifdef UTIL2
  347.     curBits = *bitBuffer << bitOffset;
  348. #else
  349.     curBits = *bitBuffer;
  350. #endif
  351.       }
  352.  
  353.       /* Return success. */
  354.  
  355.       return OK;
  356.     }
  357.   }
  358.  
  359.   /* Return underflow error. */
  360.  
  361.   return UNDERFLOW;
  362. }
  363.  
  364.  
  365. /*
  366.  *--------------------------------------------------------------
  367.  *
  368.  * get_extra_bit_info --
  369.  *
  370.  *    Parses off extra bit info stream into dynamically 
  371.  *      allocated memory. Extra bit info is indicated by
  372.  *      a flag bit set to 1, followed by 8 bits of data.
  373.  *      This continues until the flag bit is zero. Assumes
  374.  *      that bit stream set to first flag bit in extra
  375.  *      bit info stream.
  376.  *
  377.  * Results:
  378.  *    Pointer to dynamically allocated memory with extra
  379.  *      bit info in it. Flag bits are NOT included.
  380.  *
  381.  * Side effects:
  382.  *    Bit stream irreversibly parsed.
  383.  *
  384.  *--------------------------------------------------------------
  385.  */
  386.  
  387. char *get_extra_bit_info ()
  388. {
  389.   int size, marker;
  390.   char *dataPtr;
  391.   unsigned int data;
  392.  
  393.   /* Get first flag bit. */
  394.   get_bits1(data);
  395.  
  396.   /* If flag is false, return NULL pointer (i.e. no extra bit info). */
  397.  
  398.   if (!data) return NULL;
  399.  
  400.   /* Initialize size of extra bit info buffer and allocate. */
  401.  
  402.   size = EXT_BUF_SIZE;
  403.   dataPtr = (char *) malloc(size);
  404.  
  405.   /* Reset marker to hold place in buffer. */
  406.  
  407.   marker = 0;
  408.  
  409.   /* While flag bit is true. */
  410.  
  411.   while (data) {
  412.  
  413.     /* Get next 8 bits of data. */
  414.     get_bits8(data);
  415.  
  416.     /* Place in extra bit info buffer. */
  417.  
  418.     dataPtr[marker] = (char) data;
  419.     marker++;
  420.  
  421.     /* If buffer is full, reallocate. */
  422.  
  423.     if (marker == size) {
  424.       size += EXT_BUF_SIZE;
  425.       dataPtr = (char *) realloc(dataPtr, size);
  426.     }
  427.  
  428.     /* Get next flag bit. */
  429.     get_bits1(data);
  430.   }
  431.  
  432.   /* Reallocate buffer to free extra space. */
  433.  
  434.   dataPtr = (char *) realloc(dataPtr, marker);
  435.  
  436.   /* Return pointer to extra bit info buffer. */
  437.  
  438.   return dataPtr;
  439. }
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.